<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <%@ include file="/include/service-base-header.jsp" %>
    <title>系统公告</title>
    <style>
        .table tbody tr td, .table th {
            padding: 10px;
        }

        .btn {
            padding: 8px 15px;
        }
    </style>
</head>
<body>
<div class="wrapper">
    <jsp:include page="/include/left_menu.jsp">
        <jsp:param name="index" value="5"/>
    </jsp:include>
    <div class="main-panel">
        <nav class="navbar navbar-expand-lg "
             style="-webkit-box-shadow:0 0 5px rgba(40, 173, 243, 0.0);box-shadow: 0 0 5px rgba(40, 173, 243, 0.0);">
            <div class="container-fluid">
                <div class="navbar-wrapper b-navbar-wapper">
                    <a class="b-navbar-title" href="${pageContext.request.contextPath}/s/setting.do">热门比赛</a>
                    <a class="b-navbar-title" href="${pageContext.request.contextPath}/s/banner.do">轮播图</a>
                    <a class="b-navbar-title-active" href="#">系统公告</a>
                </div>
                <div class="nav-collapse justify-content-end">
                    <div>
                        <div class="toolbar_left">
                            <div class="form-inline" style="height: 1rem">

                                <div class="form-group form-group-1">
                                    <a class="btn btn-info navbar-wrapper" onclick="showDialog()" role="button"
                                       style="color: white">发布公告</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </nav>
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <div class="card card-plain">
                        <div class="card-header card-header-primary">
                            <div class="card-title">系统公告</div>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table" id="table">
                                    <thead>
                                    <tr class="col-md-12">
                                        <th class="text-primary">
                                            #
                                        </th>
                                        <th class="text-primary">
                                            发送时间
                                        </th>
                                        <th class="text-primary">
                                            标题
                                        </th>
                                        <th class="text-primary">
                                            公告内容
                                        </th>
                                    </tr>
                                    </thead>
                                    <tbody id="tbody">

                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    <nav aria-label="page">
                        <ul class="pagination justify-content-center" id="p1">
                        </ul>
                    </nav>
                </div>
            </div>
        </div>
    </div>


    <div class="modal fade" id="loginModal" tabindex="-1" role="">
        <div class="modal-dialog modal-login" role="document">
            <div class="modal-content">
                <div class="card card-signup card-plain">

                    <div class="modal-body">
                        <button type="button" class="close mt-4" data-dismiss="modal" aria-hidden="true"><i
                                class="material-icons">clear</i></button>

                        <form class="form" method="" action="">
                            <p class="description  mt-4 ml-4" style="font-size: 1.2rem;">发布公告</p>
                            <div class="card-body">
                                <div>
                                    <div class="input-group">
                                        <span style="margin-top: 0.4rem;margin-right: 0.4rem">标题</span>
                                        <div><input type="text" id="title"
                                                    style="border-color: #878787; border-style: solid; border-top-width: 0px;border-right-width: 0px; border-bottom-width: 1px;border-left-width: 0px;width: 19rem">
                                        </div>
                                    </div>
                                    <div class="input-group" style="margin-top: 1rem">
                                        <span style="margin-top: 0.4rem;margin-right: 0.4rem">内容</span>
                                        <div><TEXTAREA type="TEXTAREA" id="count"
                                                       style="width: 19rem;min-height: 5rem"></TEXTAREA>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer justify-content-end" style="padding-bottom: 1rem;margin-right: 0.5rem">
                        <button onclick="saveMessageInfo()" class="btn btn-info">发布
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

<script>
    var page_count = 10;
    function findAllMessageInfo() {
        $.ajax({
            url: baseUrl.base + baseUrl.messageInfo + "/findAllMessageInfo",
            type: 'get',
            data: {},
            success: function (data) {
                if (data.code == "0") {
                    var res = data.results;
                    cutPagination(1, res);
                    $("#p1").pagination({
                        totalData: res.length,
                        showData: page_count,
                        callback: function (idx) {
                            cutPagination(idx, res);
                        }
                    });
                } else {
                    var res = data.msg;
                    showNotification('bottom', 'right', res);
                }
            }
        });
    };
    findAllMessageInfo();

    /**
     * 分页
     * @param page 从1开始
     */
    function cutPagination(page, res) {
        var start = (page - 1) * page_count;
        var end = start + page_count;
        if (end > res.length) {
            end = res.length;
        }
        $("#table tbody").html("");
        for (var i = start; i < end; i++) {
            var tr = document.createElement("tr");

            var td = document.createElement("td");
            var tdContent = document.createTextNode(i + 1);
            td.appendChild(tdContent);
            tr.appendChild(td);

            td = document.createElement("td");
            tdContent = document.createTextNode(timestampToTime(res[i].createTime));
            td.appendChild(tdContent);
            tr.appendChild(td);

            td = document.createElement("td");
            tdContent = document.createTextNode((res[i].title));
            td.appendChild(tdContent);
            tr.appendChild(td);

            td = document.createElement("td");
            tdContent = document.createTextNode(res[i].content);
            td.appendChild(tdContent);
            tr.appendChild(td);

            document.getElementById("tbody").append(tr);
            document.getElementById("tbody").innerHTML;
        }
    }

    function showDialog() {
        $("#loginModal").modal('show');
    }

    function saveMessageInfo() {
        var datas = {};
        var title = $("#title").val();
        var count = $("#count").val();
        if (title == null || title.length <= 0) {
            showNotification('bottom', 'right', 'title不可为空');
            return;
        }
        if (count == null || count.length <= 0) {
            showNotification("bottom", "right", "内容不可为空");
            return;
        }
        datas.content = count;
        datas.title = title;
        $.ajax({
            url: baseUrl.base + baseUrl.messageInfo + "/saveMessageInfo",
            type: 'get',
            data: datas,
            success: function (data) {
                if (data.code == "0") {
                    showNotification('bottom', 'right', '公告发布成功');
                    $("#loginModal").modal('hide');
                    findAllMessageInfo();
                } else {
                    showNotification("bottom", "right", data.msg);
                    $("#loginModal").modal('hide');
                }
            }
        });

    }
</script>
</html>